<template>
  <div>
    <!-- 搜索 -->
    <el-input
      v-model="input"
      placeholder="请输入内容"
      style="width: 300px"
    ></el-input>
    <el-button icon="el-icon-search" circle @click="LookUsers"></el-button>
    <!-- 添加 -->
    <el-button type="primary" plain @click="dialogTableVisible = true"
      >添加用户</el-button
    >

    <el-table :data="tableList" style="width: 100%">
      <el-table-column prop="username" label="用户名"> </el-table-column>
      <el-table-column prop="id" label="用户ID"> </el-table-column>
      <el-table-column prop="role_name" label="昵称"> </el-table-column>
      <el-table-column prop="mobile" label="电话"> </el-table-column>
      <el-table-column prop="email" label="邮箱"> </el-table-column>
      <el-table-column prop="mg_state" label="状态">
        <!-- 修改状态 -->
        <template slot-scope="scope">
          <el-switch
            @change="modifyState(scope.row)"
            v-model="scope.row.mg_state"
            active-color="#13ce66"
            inactive-color="#ff4949"
          >
          </el-switch>
        </template>
      </el-table-column>
      <!-- 修改用户内容 -->
      <el-table-column label="修改">
        <template slot-scope="scope">
          <el-button
            icon="el-icon-edit"
            size="mini"
            @click="showEditUsers(scope.row)"
            circle
          ></el-button>
        </template>
      </el-table-column>
      <!-- 删除用户 -->
      <el-table-column label="删除">
        <template slot-scope="scope">
          <el-button
            type="danger"
            @click="deleteUsers(scope.row)"
            icon="el-icon-delete"
            circle
          ></el-button>
        </template>
      </el-table-column>
      <!-- 分配用户角色 -->
      <el-table-column label="分配">
        <template slot-scope="scope">
          <el-button
            type="warning"
            @click="disUsers(scope.row)"
            icon="el-icon-star-off"
            circle
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 页码 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[3, 5, 10, 20]"
      :page-size="100"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <!-- 添加用户弹框 -->
    <el-dialog title="添加用户" :visible.sync="dialogTableVisible">
      <el-form :model="addUserDara">
        <el-form-item label="用户名">
          <el-input
            autocomplete="off"
            v-model="addUserDara.username"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input autocomplete="off" v-model="addUserDara.mobile"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input
            autocomplete="off"
            v-model="addUserDara.password"
          ></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input autocomplete="off" v-model="addUserDara.email"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogTableVisible = false">取 消</el-button>
        <el-button type="primary" @click="postUser">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 修改用户内容的弹窗 -->
    <el-dialog
      title="修改用户"
      :visible.sync="dialogTableEditUser"
      @click="dialogTableEditUser = true"
    >
      <el-form :model="userEditData">
        <el-form-item label="用户名">
          <el-input
            autocomplete="off"
            v-model="userEditData.username"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input autocomplete="off" v-model="userEditData.mobile"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input autocomplete="off" v-model="userEditData.email"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogTableEditUser = false">取 消</el-button>
        <el-button type="primary" @click="editPutUser">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 分配用户弹窗 -->
    <el-dialog
      title="分配用户"
      :visible.sync="dialogDisEditUser"
      @click="dialogDisEditUser = true"
    >
      <el-form :model="userDisData">
        <el-form-item label="用户名">
          <el-input
            autocomplete="off"
            v-model="userDisData.username"
          ></el-input>
        </el-form-item>
      </el-form>
      <el-select v-model="roleId" placeholder="请选择">
        <el-option
          v-for="item in roleList"
          :key="item.id"
          :label="item.roleName"
          :value="item.id"
        >
        </el-option>
      </el-select>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogDisEditUser = false">取 消</el-button>
        <el-button type="primary" @click="editDisUser">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 所搜用户弹窗 -->
    <el-dialog title="用户详情" :visible.sync="dialogLookUsers">
      <el-form :model="LooksUsers">
        <el-form-item label="用户名">
          <el-input
            autocomplete="off"
            v-model="LooksUsers.username"
            disabled
          ></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input
            autocomplete="off"
            v-model="LooksUsers.mobile"
            disabled
          ></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input
            autocomplete="off"
            v-model="LooksUsers.email"
            disabled
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogLookUsers = false"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      total: 0, //一共多少条数据  ---  从0开始计数
      pageSize: 3, //每一页刷新多少条数据
      tableList: [], //储存用户的数组
      //添加用户
      addUserDara: {
        username: "", //用户名
        password: "", //密码
        mobile: "", //手机号
        email: "", //邮箱
      },
      dialogTableVisible: false, //添加用户的弹窗
      dialogTableEditUser: false, //修改用户的弹窗
      userEditData: {}, //修改用户的内容
      //用户分配
      dialogDisEditUser: false,
      userDisData: {},
      roleList: [],
      roleId: "",
      //搜索
      input: "",
      LooksUsers: {},
      dialogLookUsers: false,
    };
  },
  methods: {
    //搜索
    async LookUsers() {
      this.dialogLookUsers = true;
      var { data } = await this.$req.get("/users/" + this.input);
      if (data.meta.status == 200) {
        this.$message({
          showClose: true,
          message: "恭喜你，修改内容成功成功",
          type: "success",
        });
        this.LooksUsers = data.data;
        console.log(this.LooksUsers);
      } else {
        this.$message.error(data.meta.msg);
      }
    },
    //分配角色
    async disUsers(userinfo) {
      this.userDisData = userinfo;
      this.role_name = this.userDisData.role_name;
      // console.log(this.role_name);

      this.dialogDisEditUser = true;
      //获取所有角色信息
      var { data } = await this.$req.get("roles");
      // console.log(data.data);
      this.roleList = data.data;
    },
    //分配角色--点击确定之后修改的
    async editDisUser() {
      // console.log(this.userDisData);
      // console.log(this.roleId);
      var { data } = await this.$req.put(`/users/${this.userDisData.id}/role`, {
        rid: this.roleId,
      });
      if (data.meta.status == 200) {
        this.$message({
          showClose: true,
          message: "恭喜你，设置角色成功",
          type: "success",
        });
        this.dialogDisEditUser = false;
        this.getUsers(1, 3);
      } else {
        this.$message.error(data.meta.msg);
      }
    },
    //修改用户内容
    async editPutUser() {
      var { data } = await this.$req.put(
        "/users/" + this.userEditData.id,
        this.userEditData
      );
      this.dialogTableEditUser = false;
      if (data.meta.status == 200) {
        this.$message({
          showClose: true,
          message: "恭喜你，修改内容成功成功",
          type: "success",
        });
      } else {
        this.$message.error(data.meta.msg);
        this.getUsers();
      }
    },
    showEditUsers(userinfo) {
      this.userEditData = userinfo;
      console.log(userinfo);
      this.dialogTableEditUser = true;
    },
    //添加用户
    async postUser() {
      // console.log(this.addUserDara);
      var { data } = await this.$req.post("/users", this.addUserDara);
      if (data.meta.status == 201) {
        this.$message({
          showClose: true,
          message: "恭喜你，用户添加成功",
          type: "success",
        });
        this.getUsers();
        this.addUserDara = {};
        this.dialogTableVisible = false;
      } else {
        this.$message.error(data.meta.msg);
      }
    },
    //修改用户状态
    async modifyState(row) {
      // console.log(row);
      var { data } = await this.$req.put(
        `users/${row.id}/state/${row.mg_state}`
      );
      // console.log(data);
      if (data.meta.status == 200) {
        this.$message({
          showClose: true,
          message: "恭喜你，修改状态成功",
          type: "success",
        });
      } else {
        this.$message.error(data.meta.msg);
      }
    },
    //删除用户
    async deleteUsers(userinfo) {
      // console.log(userinfo);
      var { data } = await this.$req.delete("/users/" + userinfo.id);
      if (data.meta.status == 200) {
        this.$message({
          showClose: true,
          message: "恭喜你，删除用户成功",
          type: "success",
        });
        this.getUsers();
      } else {
        this.$message.error(data.meta.msg);
      }
    },

    //设置用户列表每一页的条数
    async handleSizeChange(pagesize) {
      this.getUsers(1, pagesize);
    },
    //设置跳转到第几页
    async handleCurrentChange(pagenum) {
      this.getUsers(pagenum, this.pageSize);
    },
    //公共代码  --- 获取用户数据列表
    //pagenum  当前页码   pagesize每页显示条数
    async getUsers(pagenum , pagesize ) {
      //获取用户数据
      var { data } = await this.$req.get(
        `/users?pagenum=${pagenum}&pagesize=${pagesize}`
      );
      console.log(data);
      if (data.meta.status == 200) {
        //渲染页面的数组为都断获取的
        this.tableList = data.data.users;
        console.log(this.tableList);
        //总数
        this.total = data.data.total;
      } else {
        this.$message({
          type: "error",
          message: data.meta.msg,
        });
      }
    },
  },

  async mounted() {
    //获取用户数据
    // console.log(this.tableList);
    this.getUsers(1, 3);
  },
};
</script>

<style>
</style>